﻿Imports System.Reflection
Imports System.IO

Imports Awesomium.Windows.Controls
Imports Awesomium.Core
Imports System.Runtime.InteropServices
Imports System.Windows.Interop

Class MainWindow

#Region "Win32 API Hooks"
    ' Define the Win32 API methods we are going to use
    <DllImport("user32.dll")> _
    Private Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function InsertMenu(ByVal hMenu As IntPtr, ByVal wPosition As Int32, ByVal wFlags As Int32, ByVal wIDNewItem As Int32, ByVal lpNewItem As String) As Boolean
    End Function

    ''' Define our Constants we will use
    Public Const WM_SYSCOMMAND As Int32 = &H112
    Public Const MF_SEPARATOR As Int32 = &H800
    Public Const MF_BYPOSITION As Int32 = &H400
    Public Const MF_STRING As Int32 = &H0


    ' The constants we'll use to identify our custom system menu items
    Public Const _SettingsSysMenuID As Int32 = 1000
    Public Const _AboutSysMenuID As Int32 = 1001

    Public ReadOnly Property Handle() As IntPtr
        Get
            Return New WindowInteropHelper(Me).Handle
        End Get
    End Property

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim systemMenuHandle As IntPtr = GetSystemMenu(Me.Handle, False)

        InsertMenu(systemMenuHandle, 5, MF_BYPOSITION Or MF_SEPARATOR, 0, String.Empty)
        ' <-- Add a menu seperator
        InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _SettingsSysMenuID, "Settings...")
        InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _AboutSysMenuID, "About...")

        ' Attach our WndProc handler to this Window
        Dim source As HwndSource = HwndSource.FromHwnd(Me.Handle)
        source.AddHook(New HwndSourceHook(AddressOf WndProc))

    End Sub

    Private Function WndProc(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr, ByRef handled As Boolean) As IntPtr
        ' Check if a System Command has been executed
        If msg = WM_SYSCOMMAND Then
            ' Execute the appropriate code for the System Menu item that was clicked
            Select Case wParam.ToInt32()
                Case _SettingsSysMenuID
                    ShowSettings()
                    handled = True
                    Exit Select
                Case _AboutSysMenuID
                    MessageBox.Show("GooMUI v 1.0 - by Master_T" & vbCrLf & vbCrLf & "http://goomui.codeplex.com", "About", MessageBoxButton.OK, MessageBoxImage.None)
                    handled = True
                    Exit Select
            End Select
        End If

        Return IntPtr.Zero
    End Function
#End Region
    

    Private ReadOnly Property ExecutableDirectory As String
        Get
            Dim l = Assembly.GetExecutingAssembly().Location
            ExecutableDirectory = l.Substring(0, l.LastIndexOf("\"))
        End Get
    End Property

    Private Property MyWebControl As WebControl
    Private Property MySettingsControl As Control

    Private ReadOnly Property MarginOffset As Integer
        Get
            Select Case My.Settings.Zoom
                Case 100
                    Return -26
                Case Is < 100
                    Return -20
                Case Else
                    Return -35
            End Select
        End Get
    End Property

    Private Property RenderedOnce As Boolean = False


    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        AddHandler Me.Loaded, New RoutedEventHandler(AddressOf Window1_Loaded)

        MySettingsControl = gbSettings

        Me.Title = "GooMUI"



        comboStart.SelectedIndex = My.Settings.StartPage
        cbRememberSize.IsChecked = My.Settings.RememberSize

        If Not Directory.Exists(ExecutableDirectory & "\cache") Then
            Directory.CreateDirectory(ExecutableDirectory & "\cache")
        End If

        Dim awesettings As New WebConfig
        WebCore.Initialize(awesettings)

        Dim ww As New WebControl
        MyWebControl = ww

        Dim sespref As New WebPreferences
        sespref.Databases = True
        sespref.Plugins = True
        sespref.AllowInsecureContent = True


        ww.WebSession = WebCore.CreateWebSession(ExecutableDirectory & "\cache", sespref)
        g1.Children.Add(ww)


        comboZoom.SelectedIndex = If(My.Settings.Zoom = 100, 1, If(My.Settings.Zoom < 100, 0, 2))
        ww.Source = DirectCast(DirectCast(comboStart.SelectedItem, ComboBoxItem).Tag, String).ToUri


        AddHandler MyWebControl.LoadingFrameComplete, AddressOf Me.resized2


        HideSettings()
    End Sub

    Private Sub Window_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
        My.Settings.LastHeight = Me.ActualHeight
        My.Settings.LastWidth = Me.ActualWidth
        My.Settings.Save()

        WebCore.Shutdown()
    End Sub


    Private Sub cbRememberSize_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        My.Settings.RememberSize = cbRememberSize.IsChecked.Value
        My.Settings.LastHeight = Me.ActualHeight
        My.Settings.LastWidth = Me.ActualWidth
        My.Settings.Save()
    End Sub

    Private Sub comboStart_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        My.Settings.StartPage = comboStart.SelectedIndex
        My.Settings.Save()
    End Sub

    Private Sub saveSettings_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        HideSettings()
    End Sub

    Private Sub ShowSettings()
        MyWebControl.Visibility = Windows.Visibility.Hidden
        MySettingsControl.Visibility = Windows.Visibility.Visible
    End Sub

    Private Sub HideSettings()
        MyWebControl.Visibility = Windows.Visibility.Visible
        MySettingsControl.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub comboZoom_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        Dim selectedZoom = DirectCast(DirectCast(comboZoom.SelectedItem, ComboBoxItem).Tag, String)
        My.Settings.Zoom = Integer.Parse(selectedZoom)
        My.Settings.Save()
        MyWebControl.Zoom = My.Settings.Zoom
        MyWebControl.Margin = New Thickness(0, MarginOffset, 0, 0)
    End Sub

    Private Sub resized2(ByVal sender As Object, ByVal e As FrameEventArgs)
        MyWebControl.Zoom = My.Settings.Zoom
        MyWebControl.Margin = New Thickness(0, MarginOffset, 0, 0)
        If My.Settings.RememberSize AndAlso My.Settings.LastWidth > 0 AndAlso My.Settings.LastHeight > 0 Then
            Me.Width = My.Settings.LastWidth
            Me.Height = My.Settings.LastHeight
        Else
            Me.Width = 1000
            Me.Height = 750.0
        End If
    End Sub



End Class
